//@ts-ignore
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from './router'

import {useUserStore} from '@/stores/index.ts'
import { API_URL } from './util/SysConfig'
const whiteList = ['/login', '/404','/401']// no redirect whitelist
import {useParamStore} from '@/stores/index'
import { useRoute } from 'vue-router'
import { getCurrentURI } from './util/URLTool'


//配置npregress
NProgress.configure({
    ease:'ease',
    speed:500
})

router.beforeEach((to,from,next)=>{
    NProgress.start();
    window.scrollTo(0,0);

    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
        next()
        return;
    }
    
    //判断用户是否登录
    const userStore = useUserStore();
    if (userStore.staffId) {
        next()
    } else {

        const paramStore = useParamStore();
         const uri = getCurrentURI();

        //这里直接重定向到后台api进行登录
        const url = window.location.href;
        paramStore.setRedirectURL(url);
        paramStore.setRedirectURI(uri);
        next('/login')
        
    }  
});


router.afterEach((to,from)=>{
    NProgress.done();
});